# -*- coding: utf-8 -*-
import subprocess
from data import analysis
from command import adb


def get_content(command):
    """

    :param command: 通过adb获取数据的命令
    :return: 完整的性能数据
    """
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    content = process.communicate()[0]
    return content


def get_data(opt_type, pid):

    if pid is not None:
        if opt_type == "cpu":
            # 获取cpu数据的adb命令
            command_cpu = adb.get_cpu + pid
            # 传入命令，获得完整数据
            content_cpu = get_content(command_cpu)
            # 过滤数据
            cpu_info = analysis.clean_data('cpu', content_cpu)
            return cpu_info

        if opt_type == "memory":
            command_mem = adb.get_memory + pid
            content_mem = get_content(command_mem)
            mem_info = analysis.clean_data('memory', content_mem)
            return mem_info

        if opt_type == "battery":
            command_bat = adb.get_battery
            content_bat = get_content(command_bat)
            bat_info = analysis.clean_data('battery', content_bat)
            return bat_info

        if opt_type == "temperature":
            command_temperature = adb.get_temperature
            content_temperature = get_content(command_temperature)
            temper_info = analysis.clean_data('temperature', content_temperature)
            return temper_info

        if opt_type == "flow":
            command_flow = "adb shell cat /proc/" + pid + '/net/dev | grep wlan0'
            content_flow = get_content(command_flow)
            flow_info = analysis.clean_data('flow', content_flow)
            return flow_info
    else:
        return 0
